<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/4
 * Time: 18:20
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\view;
use app\common\model\Share;
class Base extends Controller
{
    //使用tp框架提供的构造函数验证session中是否有用户的信息的唯一标识user_id
    public function _initialize()
    {
        $user_id = session('user_id');

        if (!$user_id) {
            //获取当前网页，授权后跳回
            $path = $_SERVER['REQUEST_URI'];
            $studentId = $this->request->get('user_id');
            //跳转到微信授权
            $this->redirect('/index/Auth/index?path='.$path.'&student_id='.$studentId);
        }else{
            $openid = Db::name('people')->where('id',$user_id)->value('openid');
            $info = info($openid);
            if($info['subscribe'] !=1){
                echo "<div style='position:fixed;bottom:100px;left: 25%'><img src='/uploads/qrcode/15676079484280578001.jpg'></div>

                    <div style='position:fixed;bottom:500px;left: 30%;font-size: 50px;color: #00FF00'>长按识别二维码</div>";
                die;
            }

            $studentId = $this->request->get('user_id');
            if($studentId){
                Db::name('people')->where('id',$user_id)->update(['section_id'=>$studentId]);
            }
        }


    }
    public function add_mobile(){
        $user_id = session('user_id');
        $mobile=$this->request->post();
        $find=Db::name("people")->where(array("mobile"=>$mobile['mobile']))->find();
        if(!$find){
            $res=Db::name("people")->where(array("id"=>$user_id))->update(array("mobile"=>$mobile['mobile']));
            if($res){
                return 200;
            }else{
                return 400;
            }
        }else{
            return 401;
        }

    }

}